<?php
namespace Info\Controller;
use Info\Controller\CommonController;
use Common\Model\SearchModel;
use Common\Model\InfoContentModel;
use Common\Model\InfoCategoryModel;
class SearchController extends CommonController {

    public function Index(){
        $qword = I('w');
        $page = I('p',0,'int');
        $page = $page > 0 ? $page - 1 : 0;

        $searchModel = new SearchModel();
        $data = $searchModel->search($qword);

        if($data['idArr']){
            $startPage = $page * C('PAGE_SIZE_SEARCH');
            $idArr = array_slice($data['idArr'],$startPage,C('PAGE_SIZE_SEARCH'));

            $infoModel = new InfoContentModel();
            $infoData = $infoModel->getInfoByIds($idArr);
        }else{
            $infoData = array();
        }

        if($data['word']){
            $replaceWord = array();
            foreach($data['word'] as $k => $v){
                $replaceWord[] = "<span>" . $v ."</span>";
            }
        }

        $categoryModel = new InfoCategoryModel();
        $topCategory = $categoryModel->getCategoryByPid(0);

        $pageShow = $this->PageShow($data['count'],'search',C('PAGE_SIZE_SEARCH'));

        $replaceWord = isset($replaceWord) ? $replaceWord : array();
        $this->assign('infoData',$infoData)
             ->assign('count',$data['count'])
             ->assign('word',$data['word'])
             ->assign('qword',$qword)
             ->assign('pageShow',$pageShow)
             ->assign('topCategory',$topCategory)
             ->assign('replaceWord',$replaceWord);

        $this->display(":search");

    }
    
    public function getPhoneInfo(){
        $phone = I('phone','','int');
        if(!empty($phone)){
            $returnStr = $this->curlGetPhonePosition($phone);
        }
        $times = $this->getInfosByPhone($phone);
        $returnStr = isset($returnStr) ? $returnStr : '';
        $returnStr .= "<br /><span>共发信息<strong>{$times}</strong>条</span>";
        echo $returnStr;
    }

    public function byPhone(){
        $phone = I('phone');
        $page = I('p',0,'int');
        $page = $page > 0 ? $page - 1 : 0;

        $infoModel = new InfoContentModel();
        $data = $infoModel->searchByPhone($phone,$page);
        $infoData = isset($data['data']) ? $data['data'] : array();

        $categoryModel = new InfoCategoryModel();
        $topCategory = $categoryModel->getCategoryByPid(0);

        $pageShow = $this->PageShow($data['count'],'searchByPhone',C('PAGE_SIZE_SEARCH'));

        $replaceWord = isset($replaceWord) ? $replaceWord : array();

        $this->assign('infoData',$infoData)
            ->assign('count',$data['count'])
            ->assign('word',$data['word'])
            ->assign('qword',$phone)
            ->assign('pageShow',$pageShow)
            ->assign('topCategory',$topCategory)
            ->assign('replaceWord',$replaceWord);

        $this->display(":search");
    }


    public function getPhonePosition(){
        $phone = I('phone','','int');
        if(!empty($phone)){
            $position = $this->curlGetPhonePosition($phone);
        }
        echo $position;
    }


    public function curlGetPhonePosition($phone)
    {
//        $url = C("get_phone_info_url") .$phone;
//        $request = curl($url);

        //api type 2

        $ch = curl_init();
        $url = C("GET_PHONE_INFO_URL") .$phone;
        $header = array(
            'apikey:'. C('PHONE_API_KEY'),
        );
        curl_setopt($ch, CURLOPT_HTTPHEADER  , $header);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        // 执行HTTP请求
        curl_setopt($ch , CURLOPT_URL , $url);
        $res = curl_exec($ch);
        $res = json_decode($res,true);

        $returnStr = '';
        if($res && $res['msg'] == 'succeed'){
            $returnStr = $res['data']['province'] .'/'. $res['data']['city'] .' '. $res['data']['operator'];
        }else{
            $returnStr = "查询异常请稍后再试...";
            if(isset($res['retMsg'])){
                $returnStr = $res['retMsg'];
            }
        }
//        var_dump($res);die;
//        if(!empty($request)){
//            $xml = simplexml_load_string($request);
//            $retmsg = $xml->retmsg;
//            if($retmsg == 'OK'){
//                $city = $xml->city;
//                $province = $xml->province;
//                $supplier = $xml->supplier;
//                $returnStr = str_replace(' ','',"{$province}-{$city}"). " $supplier";
//            }else{
//                $returnStr = "查询异常请稍后再试...";
//            }
//        }else{
//            $returnStr = "查询异常请稍后再试...";
//        }
        return $returnStr;
    }




}